class Singleton:
    """单列模式"""
    """这个类只能创建一个实例， 没有就创建， 有返回"""
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
        return cls._instance


class Word(str):
    """判断word实例和传入的字符串的长度是否相等"""
    def __eq__(self, other):
        return len(self) == len(other)


# 比较运算符
class Complete(str):
    def __eq__(self, other):
        """定义等于操作的运算符(==)的行为"""
        return len(self) == len(other)

    def __ne__(self, other):
        """定义不等于(!=)操作的运算符"""
        return self != other

    def __lt__(self, other):
        """定义小于(<)操作运算符的行为"""
        return self < other

    def __gt__(self, other):
        """定义大于操作符(>)的行为"""
        return self > other

    def __le__(self, other):
        """定义小于等于(<=)的操作符的行为"""
        return self <= other

    def __ge__(self, other):
        """定义大于等于(>=)等于操作符的行为"""


# 一元操作符
class Int(object):
    """操作读的魔法方法"""
    def __pos__(self):
        """实现取正操作"""
        pass

    def __neg__(self):
        """实现取负操作"""
        pass

    def __abs__(self):
        """实现取绝对值函数abs()操作"""
        pass

    def __invert__(self):
        """实现取反的操作"""
        pass

    def __floor__(self):
        """实现向下取整的操作"""
        pass

    def __ceil__(self):
        """向上取取整"""
        pass

    def __trunc__(self):
        """距离最近的整数"""
        pass

    def __add__(self, other):
        """加法操作"""
        pass

    def __sub__(self, other):
        """减法操作"""
        pass

    def __floordiv__(self, other):
        """实现//操作符的整数除法"""
        pass

    def __truediv__(self, other):
        """实现/操作符的除法"""
        pass

    def __iadd__(self, other):
        """实现+="""
        pass

    def __isub__(self, other):
        """实现 -="""
        pass

    def __imul__(self, other):
        """实现*="""
        pass

    def __ifloordiv__(self, other):
        """实现 //="""
        pass

    def __itruediv__(self, other):
        """实现 /="""
        pass

    def __imod__(self, other):
        """实现%="""
        pass

    def __init__(self):
        """实现到int的类型转换"""
        pass

    def __float__(self):
        """实现到float的雷翔转换"""
        pass

    def __complex__(self):
        """实现到complex的类型转换"""
        pass

    def __oct__(self):
        """实现到八进制的的类型转换"""
        pass

    def __hex__(self):
        """实现到十六进制的类型转换"""
        pass


# 1、__str__/__repr__
"""__str__定义对类的实例的行为。而__repr__定义对类的实例调用repr()的行为，
这两种的区别就是repr()面向机器，str面向人。定义类的输出的时候经常会使用这两个其中的魔法。
"""


class Apple(object):
    def __init__(self, name, size):
        self.name = name
        self.size = size

    def __str__(self):
        return "{}:{}".format(self.name, self.size)


# 2、__getattr__/__setattr__/__delattr__
"""这三个方法你可以随时的修改，删除，添加类属性或者值"""


class People(object):
    def __init__(self, name, size):
        self.name = name
        self.size = size

    def __getattr__(self, name):
        return self.name

    def __setattr__(self, name, value):
        self.__dict__[name] = "people -{}".format(value)


# a = People("A", "20g")
# print(a.__dict__)


class AccessCounter(object):
    ''' 一个包含了一个值并且实现了访问计数器的类每次值的变化都会导致计数器自增'''
    def __init__(self, val):

            super(AccessCounter, self).__setattr__('counter', 0)

            super(AccessCounter, self).__setattr__('value', val)

    def __setattr__(self, name, value):

        if name == 'value':

                super(AccessCounter, self).__setattr_('counter', self.counter + 1)

        # 使计数器自增变成不可避免

        # 如果你想阻止其他属性的赋值行为

        # 产生 AttributeError(name) 就可以了

        super(AccessCounter, self).__setattr__(name, value)

    def __delattr__(self, name):

        if name == 'value':

                super(AccessCounter, self).__setattr('counter', self.counter + 1)

                super(AccessCounter, self).__delattr(name)












